<?php

namespace addons\shopro\controller;

use addons\shopro\exception\Exception;
use addons\shopro\services\CategoryService;
use app\admin\model\shopro\cats\Breed;

class Cats extends Base
{

    protected $noNeedLogin = ['catLists', 'catDetails', 'catBreed'];
    protected $noNeedRight = ['*'];

    /**
     * 喵咪列表
     * @return void
     */
    public function catLists()
    {
        if (request()->isPost()) {
            $this->relationSearch = true;

            //空就查询所有分类
            $pid = $this->request->post('pid/d');
            $cat_code = $this->request->post('cat_code');
            // 获取分页参数
            $page = $this->request->post('page/d', 1);
            $limit = $this->request->post('limit/d', 1000);
            $where = !empty($pid) ? ['pid' => $pid] : [];
            $cat_code = !empty($cat_code) ? '%' . $cat_code . '%' : '2%';
            $params = $this->request->post();
            if (empty($params['order'])) {
                $order = [];
            } else {
                $arr = explode(',', $params['order']);
                $str = $arr[0];
                $sort = $arr[1];
                $order = [$str => $sort];
            }

            $catsModel = new \addons\shopro\model\Cats();
            $lists = $catsModel
                ->with('catBreed')
                ->where($where)
                ->whereLike('cat_code', $cat_code)
                ->where(['status' => 1])
                ->where(['type' => 1])
                ->field('id,pid,image,name,age,sales,gender,price,price_status,introduce,create_time,cat_code,status,type')
                ->order($order)
                ->paginate($limit, false, ['page' => $page]);
            if (!empty($lists)) {
                //猫咪列表
                foreach ($lists as $value) {
                    $value['price'] = $value['price_status'] == 1 ? '' : bcdiv($value['price'], 100, 2);
                    $value['breed_name'] = $value['cat_breed']['name'];
                    $value['image'] = cdnurl($value['image'], true);
                    $value['create_time'] = date('Y-m-d H:i:s', $value['create_time']);
                }
            }
            $result['lists'] = $lists;
            $this->success('猫咪列表', $result);
        }

        $this->error('请检查您的请求方式!');
    }

    /*
     * 猫咪详情
     * */
    public function catDetails参考()
    {
        if (request()->isPost()) {
            $id = $this->request->post('id/d');
            $catsModel = new \addons\shopro\model\Cats();
            $info = $catsModel->with('cat_breed')
                ->field('id,pid,name,age,gender,cat_code,breed as breed_name,image,images,video_path,price,introduce,introduce_image,createtime')
                ->where('id', $id)->find();
            $info['gender'] = $info['gender'] == 1 ? '公' : '母';
            $info['image'] = cdnurl($info['image'], true);
            $arr = explode(',', $info['images']);
            foreach ($arr as &$val) {
                $val = cdnurl($val, true);
            }
            $info['images'] = $arr;
            $info['breed_name'] = $info['cat_breed']['name'];
            $info['createtime'] = date('Y-m-d H:i', time());
            $this->success('猫咪详情', $info);
        }
    }


    /**
     * 猫咪详情
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @Author ycr <E-mail:yuncran@126.com>
     */
    public function catDetails()
    {
        if (request()->isPost()) {
            $id = $this->request->post('id/d');
            $catsModel = new \addons\shopro\model\Cats();
            $info = $catsModel->with('cat_breed')
                ->field('id,pid,name,age,gender,image,images,video_path,price,introduce,
                introduce_image,create_time,guide_introduce,adress as address,cat_code,type')
                ->where('id', $id)->find();
            $info['breed_name'] = $info['cat_breed']['name'];
            $this->success('猫咪详情', $info);
        }
        $this->error('请检查请求方式！');
    }


    /**
     * 喵咪分类列表
     * @return void
     * @Author ycr <E-mail:yuncran@126.com>
     */
    public function catBreed()
    {
        $data = (new CategoryService())->getCatsCategory();
        $this->success('猫咪品种列表', $data);
//        $this->success('猫咪品种列表',$this->catbreedList());
    }

    /*公用*/
    public function catbreedList()
    {
        $breedModel = new \addons\shopro\model\Breed();
        $lists = $breedModel->where('status', 1)->select();
        foreach ($lists as $key => $val) {
            $lists[$key]['image'] = cdnurl($val['image'], true);
        }
        return $lists;
    }

}